package org.tissou.client.Services;

import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.rpc.AsyncCallback;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Claire
 * Date: 19-Mar-2009
 * Time: 21:54:11
 * To change this template use File | Settings | File Templates.
 */
public class ArbreDeProprietaires extends Tree{

  private AsyncCallback<List<ProprietaireInfos>> chargeageCallback = new AsyncCallback<List<ProprietaireInfos>>() {

    public void onFailure(Throwable caught) {
      addItem("Erreur mec. Arbre proprietaire non charge." + caught.getMessage());
    }

    public void onSuccess(List<ProprietaireInfos> result) {
      for (ProprietaireInfos p : result){
        TreeItem t = new TreeItem(p.getNom());
        t.setUserObject(p);
        addItem(t);
      }
    }
  };

  public ArbreDeProprietaires() {
    super();
    chargerLEnsembleDesProprietaire();

  }

  private void chargerLEnsembleDesProprietaire() {
    EnsemblePropriosService.App.getInstance().donnerListeProprietaires(chargeageCallback);
  }

    public ProprietaireInfos donneLeProprietaireSelectionne() throws MonException{
    try {
      return (ProprietaireInfos) this.getSelectedItem().getUserObject();
    } catch (NullPointerException e){
      throw new MonException("Tu n'as pas selectionne de proprietaire !", e);
    }
  }



  public ProprietaireInfos donneLeProprietaire(int index){
     return (ProprietaireInfos) this.getItem(index).getUserObject();
  }

  public void Refresh(){
    this.clear();
    chargerLEnsembleDesProprietaire();
  }

  public int NbreProprietaire(){
    return this.getItemCount();
  }
}
